package com.wukaige.goods.controller;


import com.wukaige.goods.config.JwtUtils;
import com.wukaige.goods.config.ResultEntity;
import com.wukaige.goods.entity.Address;
import com.wukaige.goods.entity.City;
import com.wukaige.goods.service.IAddressService;
import com.wukaige.goods.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wukaige
 * @since 2021-07-21
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    IAddressService addressService;

    @Autowired
    ICityService cityService;

    @RequestMapping("commitcity")
    public ResultEntity commitcity(@RequestBody Address address, HttpServletRequest request){
        List<String> list = new ArrayList();
        String header = request.getHeader("token");
        for (Integer cityid : address.getCityid()) {
            System.out.println(cityid);
            City citys = cityService.getcitybyid(cityid);
            System.out.println(citys);
            System.out.println(citys.getCityname());
            list.add(citys.getCityname());
        }
        String addressname = list.get(0)+list.get(1)+list.get(2);
        System.out.println(addressname);
        JwtUtils jwtUtils = new JwtUtils();
        String userIdByToken = jwtUtils.getUserIdByToken(header);
        Address address1 = new Address();
        address1.setUid(Integer.parseInt(userIdByToken));
        address1.setAddressname(addressname);
        boolean save = addressService.save(address1);
        return ResultEntity.ok("200",save);
    }
    @RequestMapping("getaddress")
    public List<Address> getaddress(HttpServletRequest request){
        String header = request.getHeader("token");
        JwtUtils jwtUtils = new JwtUtils();
        String userIdByToken = jwtUtils.getUserIdByToken(header);
        List<Address> list = addressService.getaddressbyuid(Integer.parseInt(userIdByToken));
        return list;
    }
}

